package com.dsteiner.widgets.client;

import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.SimplePanelExt;
import com.dsteiner.widgets.client.controller.NineBoxController;
import com.dsteiner.widgets.client.resources.NineBoxCssResource;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class NineBoxWidget extends LazyWidget{

	public static class NineBoxModel {
		private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(
				this);

		public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
			propertyChangeSupport.registerPropertyChangeListener(propertyName, listener);
		}
		
		private IWidget contentWidget;

		public IWidget getContentWidget() {
			return contentWidget;
		}

		public void setContentWidget(IWidget contentWidget) {
			IWidget oldContentWidget = this.contentWidget;
			this.contentWidget = contentWidget;
			propertyChangeSupport.firePropertyChange("contentWidget", oldContentWidget, contentWidget);
		}
		private NineBoxCssResource nineBoxCssResource;

		public NineBoxCssResource getNineBoxCssResource() {
			return nineBoxCssResource;
		}

		public void setNineBoxCssResource(NineBoxCssResource nineBoxCssResource) {
			NineBoxCssResource oldNineBoxCssResource = this.nineBoxCssResource;
			this.nineBoxCssResource = nineBoxCssResource;
			propertyChangeSupport.firePropertyChange("nineBoxCssResource", oldNineBoxCssResource, nineBoxCssResource);
		}
	}
	
	private final NineBoxModel nineBoxModel;
	private NineBoxController nineBoxController = new NineBoxController();
	public NineBoxWidget(NineBoxModel nineBoxModel) {
		this.nineBoxModel = nineBoxModel;
	}
	
	@Override
	public Widget createContentWidget() {
		FlowPanel mainPanel = new FlowPanel();
		SimplePanelExt topLeft = new SimplePanelExt();
		SimplePanelExt top = new SimplePanelExt();
		SimplePanelExt topRight = new SimplePanelExt();
		SimplePanelExt left = new SimplePanelExt();
		SimplePanelExt right = new SimplePanelExt();
		SimplePanelExt bottomLeft = new SimplePanelExt();
		SimplePanelExt bottomRight = new SimplePanelExt();
		SimplePanelExt bottom = new SimplePanelExt();
		SimplePanelExt contentPanelHolder = new SimplePanelExt();
		
		mainPanel.add(contentPanelHolder);
		mainPanel.add(top);
		mainPanel.add(left);
		mainPanel.add(right);
		mainPanel.add(bottom);
		mainPanel.add(bottomLeft);
		mainPanel.add(topLeft);
		mainPanel.add(topRight);
		mainPanel.add(bottomRight);
		
		nineBoxController.registerNineBoxModel(nineBoxModel);
		nineBoxController.registerNineBoxWidget(this);
		nineBoxController.registerContentPanelHolder(contentPanelHolder);
		nineBoxController.registerBottom(bottom);
		nineBoxController.registerBottomLeft(bottomLeft);
		nineBoxController.registerBottomRight(bottomRight);
		nineBoxController.registerLeft(left);
		nineBoxController.registerRight(right);
		nineBoxController.registerTop(top);
		nineBoxController.registerTopRight(topRight);
		nineBoxController.registerTopLeft(topLeft);
		return mainPanel;
	}

}
